package com.ksyun.cloudhostalarmprocessing.service.Impl;

import com.ksyun.cloudhostalarmprocessing.entity.Metrics;
import com.ksyun.cloudhostalarmprocessing.mapper.MetricsMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ElasticsearchService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    MetricsMapper metricsMapper;
    public void bulkImport() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        List<Metrics> metrics = metricsMapper.selectAll1();
        for (Metrics metric : metrics) {
            IndexRequest request = new IndexRequest("metrics").source(
                    "{" +
                            "\"metric_id\":\"" + metric.getMetricId() + "\"," +
                            "\"metric_name\":\"" + metric.getMetricName() + "\"," +
                            "\"instanceId\":\"" + metric.getInstanceId() + "\"," +
                            "\"timestamp\":\"" + metric.getTimestamp() + "\"," +
                            "\"value\":\"" + metric.getValue() + "\"," +
                            "\"hostname\":\"" + metric.getHostname() + "\"" +
                            "}", XContentType.JSON);
            bulkRequest.add(request);
        }

        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    public List<Metrics> getAllMetrics() {

        List<Metrics> result = new ArrayList<>();
        SearchRequest searchRequest = new SearchRequest("metrics");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = searchResponse.getHits().getHits();
            for (SearchHit hit : hits) {
                Metrics metrics = new Metrics();
                Map<String, Object> map = hit.getSourceAsMap();
                metrics.setMetricId((Long) map.get("metric_id"));
                metrics.setMetricName((String) map.get("metric_name"));
                metrics.setInstanceId((String) map.get("instanceId"));
                metrics.setTimestamp(LocalDateTime.parse((String) map.get("timestamp")));
                metrics.setValue((Float) map.get("value"));
                metrics.setHostname((String) map.get("hostname"));
                result.add(metrics);
            }
        } catch (IOException e) {
            // Handle the exception
            log.error("出错了");
        }
        return result;
    }
}
